package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.AdminBank;
import com.coin.dto.AdminBankDto;
import com.coin.model.R;
import com.coin.service.AdminBankService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/6/6
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/adminBanks")
@Api(tags = "公司银行卡的配置")
public class AdminBankController {

    @Autowired
    private AdminBankService adminBankService;


    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: bankCard  page
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "条件查询公司银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCard", value = "公司的银行卡"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> findByPage(@ApiIgnore Page<AdminBank> page, String bankCard) {
        Page<AdminBank> adminBankPage = adminBankService.findByPage(page, bankCard);
        return R.ok(adminBankPage);
    }

    /**
     * @Description: 新增一个银行卡
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: adminBank
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "adminBank json数据")
    })
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R add(@RequestBody @Validated AdminBank adminBank) {
        boolean result = adminBankService.saveBank(adminBank);
        return result ? R.ok() : R.fail("新增失败");
    }

    /**
     * @Description: 修改一个银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: adminBank
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "adminBank json数据")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R update(@RequestBody @Validated AdminBank adminBank) {
        boolean result = adminBankService.updateBank(adminBank);
        return result ? R.ok() : R.fail("修改失败");
    }

    /**
     * @Description: 修改银行卡的状态
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: bankId  status
     * @return: R
     */
    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation(value = "修改银行卡的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "要修改的银行卡的ID"),
            @ApiImplicitParam(name = "status", value = "要修改为的状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R changeStatus(Long bankId, Byte status) {
        boolean result = adminBankService.updateStatus(bankId, status);
        return result ? R.ok() : R.fail("修改成功");
    }

    @GetMapping("/list")
    List<AdminBankDto> getAllAdminBanks() {
        return adminBankService.getAllAdminBanks();
    }
}
